// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import TickerMgr from "../ticker/TickerMgr";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Bezier {
    private _points:Array<cc.Vec2>;
    constructor(points:Array<cc.Vec2>) {
        this._points = points;
    }
    //ratio变速倍率
    public bezier(time:number) {
        let degree = time*90;
        
        let x = this.bezierPosX(this._points.length-1, 0, Math.sin(degree*Math.PI/180));
        let y = this.bezierPosY(this._points.length-1, 0, Math.sin(degree*Math.PI/180));
        
        return new cc.Vec2(x, y);
    }

    private bezierPosX(s:number, ctrlIdx:number, time:number) {
        if (s == 1) {
            return (1 - time) * this._points[ctrlIdx].x + time * this._points[ctrlIdx + 1].x
        }
        return (1 - time) * this.bezierPosX(s - 1, ctrlIdx, time) + time * this.bezierPosX(s - 1, ctrlIdx + 1, time)
    }

    private bezierPosY(s:number, ctrlIdx:number, time:number) {
        if (s == 1) {
            return (1 - time) * this._points[ctrlIdx].y + time * this._points[ctrlIdx + 1].y
        }
        return (1 - time) * this.bezierPosY(s - 1, ctrlIdx, time) + time * this.bezierPosY(s - 1, ctrlIdx + 1, time)
    }
}